/*
 * TransferMessage.cpp
 *
 */

#include "TransportMessage.h"

#include "../UI_multiplexer.h"  // XXX

vsznDefault(TransportMessage);

TransportMessage::TransportMessage()
{
	// Default constructor is
	// necessary for serialization
}

// full constructor
TransportMessage::TransportMessage(MESSAGE_TYPE type, uint8_t subtype) :
    type(type), subtype(subtype),
    seq_num(0)
{
}

//// compatibility constructor (without subtype)
//TransportMessage::TransportMessage(MESSAGE_TYPE type, const ariba::NodeID srcNode, const string name)
//{
//  TransportMessage(type, (uint8_t) 0, srcNode, name);
//}


//TransportMessage::TransportMessage(TransportMessage &other)
//{
//    chatText = other.chatText;
//
////    senderNodeID = other.senderNodeID;
//}

TransportMessage::~TransportMessage()
{
//    // XXX
//    if ( seq_num != 0 )
//    {
//        ostringstream out;
//        out << "/// DESTRUCTOR TMSG: [" << /*type << ", " << subtype << ", " <<*/ seq_num << "]";
//        if ( type == ACK )
//            out << " [ACK]";
//        
//        UI_multiplexer::instance().write_debug(out.str());
//    }
}

TransportMessage::MESSAGE_TYPE TransportMessage::getType(){

        return (MESSAGE_TYPE) (type & MASK_TYPES);

} // getType

uint8_t TransportMessage::getSubtype(){

        return subtype;

} // getSubtype


//// XXX deprecated
//bool TransportMessage::is_ack()
//{
//    return false;
//}

// FLAGS
void TransportMessage::set_flag(TransportMessage::MESSAGE_FLAG flag)
{
    type = type | flag;
}

bool TransportMessage::has_flag(TransportMessage::MESSAGE_FLAG flag)
{
    return (type & flag) != 0;
}


// sequence numbers
omnibus_seq_num TransportMessage::get_seq_num() const
{
    return seq_num;
}

void TransportMessage::set_seq_num(omnibus_seq_num seqNum)
{
    seq_num = seqNum;
}

void TransportMessage::request_ack()
{
    set_flag(TransportMessage::ACK_REQUESTED);    
}

bool TransportMessage::ack_requested()
{
    return has_flag(TransportMessage::ACK_REQUESTED);
}
